﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBService.Admin;

public partial class administration_ManageUsers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// in fact it's removing user's reviews
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            var userManager = new UserManager();
            userManager.RemoveUserReviews((int)e.Keys["ID"]);
            gvUsers.DataBind();
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterClientScriptBlock(this, Page.GetType(), "DeleteError", "alert('Error on deleting, try again or report a bug!')", true);
        }
        finally
        {
            e.Cancel = true;
        }

    }

    /// <summary>
    /// in fact it's user banning
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvUsers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            var userManager = new UserManager();
            var row = gvUsers.Rows[e.NewEditIndex];
            var user = userManager.GetUserBy(int.Parse(row.Cells[0].Text));
            if (user != null)
            {
                Facebook.BanUser(user.FaceID.ToString());
            }
            e.Cancel = true;
            gvUsers.DataBind();
        }
        catch (Exception ex)
        {
            // TODO log exc
            e.Cancel = true;
        }
    }

    protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            var userManager = new UserManager();
            switch (e.CommandName)
            {
                case "DeleteSimilar":
                    int index = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = gvUsers.Rows[index];
                    var userid = int.Parse(row.Cells[0].Text);
                    var reviewText = (row.FindControl("lblRevText") as Label).Text;
                    if (reviewText == "")
                        return;
                    userManager.RemoveUsersSameReviews(userid, reviewText);
                    break;
                default:
                    break;
            }
            if (!new string[] { "Delete", "Edit" }.Contains(e.CommandName))
            {
                gvUsers.DataBind();
            }
        }
        catch (Exception)
        {
            //TODO Log ex.
        }
    }

    protected void Preview_Click(object sender, EventArgs e)
    {
        try
        {
            var userManager = new UserManager();
            List<UserMan> users;
            if (isTheSame.Checked)
            {
                DateTime equalPostFrom = DateTime.Parse(txtEqualsFrom.Value);
                DateTime equalpostTo = DateTime.Parse(txtEqualsTo.Value);
                users = userManager.GetUsersBy(equalPostFrom, equalpostTo).Where(y => y.Reviews > 1).ToList();
                gvUsers.Columns[8].Visible = true;
            }
            else if (String.IsNullOrEmpty(txtUserName.Value))
            {
                int postsNumber = int.Parse(txtNumberOfPosts.Value);
                DateTime postDateFrom = DateTime.Parse(txtPostsDateFrom.Value);
                DateTime postDateTo = DateTime.Parse(txtPostsDateTo.Value);
                users = userManager.GetUsersBy(postsNumber, postDateFrom, postDateTo);
            }
            else
            {
                users = userManager.GetUsersBy(txtUserName.Value);
            }
            BindRep(users);
        }
        catch (Exception ex)
        {

        }
    }

    private void BindRep(List<UserMan> users)
    {
        if (users.Count > 0)
        {
            gvUsers.Visible = true;
            lblUserNotFound.Visible = false;
            gvUsers.DataSource = users;
            gvUsers.DataBind();
        }
        else
        {
            gvUsers.Visible = false;
            lblUserNotFound.Visible = true;
        }
    }



}